package com.hq.thread.test3;

import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;

/**
 * @ClassName MyLock
 * @Description TODO
 * @Author huangzl
 * @Date 2019/4/9 15:13
 * @Version 1.0
 **/
public class MyLock {

    static int tickets = 100;
    static int count = 0;

    public static void main(String[] args) {
        int shop = 5;
        for (int i = 1; i <= shop; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (tickets > 0) {
                        synchronized (this) {
                            if (tickets > 0) {
//
                                try {
                                    Thread.sleep(new Random().nextInt(100));
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }

                                count++;
                                tickets--;
//                            System.out.println("=================>" + count);
                                System.out.println(Thread.currentThread().getName() + "\t当前票号：" + count + ",剩余票数：" + tickets);
                            }
                        }
                    }
                }
            }, "售票点" + i);
            thread.start();
        }
    }
}